package org.example.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 *
 *
 *
 */
public class Demo01JDBC {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.
        Connection connection= DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root"
        );
        //3.创建SQL语句的执行对象；
        Statement statement=connection.createStatement();
        //接收用户输入；
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username=scanner.nextLine();
        System.out.println("请输入密码：");
        String password=scanner.nextLine();
        System.out.println("请输入昵称：");
        String nickname=scanner.nextLine();
        System.out.println("请输入年龄：");
        int age = scanner.nextInt();
        //4.执行SQL语句：executeUpdate（）—执行写操作；executeQUery（）——
        //"+变量名+“ 作用
        statement.executeUpdate("INSERT INTO user(username,password,nickname,age) " + "VALUES ('"+username+"','"+password+"','"+nickname+"',"+age+")");
        System.out.println("数据插入成功;");
        //5.断开数据库连接；
        connection.close();
    }
}
